
const openEyes2=document.querySelector('.openEyes2')
const closeEyes2=document.querySelector('.closeEyes2')
const inputPasswordAgain=document.getElementsByClassName('input_password-again')[0]
openEyes2.addEventListener('click',function(){
    openEyes2.style.display='none'
    closeEyes2.style.display='flex'
    inputPasswordAgain.type='password'
})
closeEyes2.addEventListener('click',function(){
    openEyes2.style.display='flex'
    closeEyes2.style.display='none'
    inputPasswordAgain.type='text'
})

const openEyes1=document.querySelector('.openEyes1')
const closeEyes1=document.querySelector('.closeEyes1')
const inputPassword0=document.getElementsByClassName('input_password')[0]
openEyes1.addEventListener('click',function(){
    openEyes1.style.display='none'
    closeEyes1.style.display='flex'
    inputPassword0.type='password'
})
closeEyes1.addEventListener('click',function(){
    openEyes1.style.display='flex'
    closeEyes1.style.display='none'
    inputPassword0.type='text'
})
function validatePassword() {
    var password=document.querySelector(".input_password").value;
    const regex = /^.{6,64}$/;
    if (regex.test(password)) {
        document.getElementById('resultPassword').textContent = '';
    } else {
        document.getElementById('resultPassword').textContent = '密码长度需在 6 到 64 位之间';
    }
}
var inputPassword=document.querySelector('.input_password')
inputPassword.addEventListener('input',validatePassword)

function validateEmail() {
    const  email=document.querySelector(".input_txt").value;
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

    if (emailRegex.test(email)) {
        document.getElementById('resultEmail').textContent = '';
    } else {
        document.getElementById('resultEmail').textContent = '邮箱地址无效';
    }
}
const  input_email=document.querySelector(".input_txt")
input_email.addEventListener('input',validateEmail)

const passwordInput=document.querySelector('.input_password-again')
passwordInput.addEventListener('input',function(){
    const password=document.querySelector('.input_password').value
    const confirmPassword=document.querySelector('.input_password-again').value
    if(password===confirmPassword){
        document.getElementById('resultPasswordAgain').textContent='';
    }else{
        document.getElementById('resultPasswordAgain').textContent='两次输入的密码不一致，请重新输入';
    }
})


function showCustomAlert(message, duration) {
    let customAlert = document.getElementById('customAlert');
    if (!customAlert) {
        div_outter=document.createElement('div');
        customAlert = document.createElement('div');
        customAlert.id = 'customAlert';
        // customAlert.style.margin='7px 12px';
        const alertMessage = document.createElement('p');
        alertMessage.id = 'alertMessage';
        customAlert.appendChild(alertMessage);

        document.body.appendChild(customAlert);
    }

    const alertMessage = document.getElementById('alertMessage');

    alertMessage.textContent = message;
    if(alertMessage.textContent==null){
        customAlert.style.display = 'none';
    }

    customAlert.style.display = 'block';
    setTimeout(() => {
        customAlert.style.display = 'none';
    }, duration);
}
if (errorMessage && errorMessage !== '') {
    showCustomAlert(errorMessage, 3000);
}
const input_code=document.getElementsByClassName('input_code')[0]
const button=document.getElementsByClassName('button')[0]
function dosubmit(){

    if(input_email.value.trim()===''){
        event.preventDefault()
        input_email.value='请输入邮箱地址'
        input_email.style.color='red'
    }
    if(inputPassword.value.trim()===''){
        event.preventDefault()
        openEyes1.style.display='flex'
        closeEyes1.style.display='none'
        inputPassword.type='text'
        inputPassword.value='请输入密码'
        inputPassword.style.color='red'
    }
    if(passwordInput.value.trim()===''){
        event.preventDefault()
        openEyes2.style.display='flex'
        closeEyes2.style.display='none'
        passwordInput.type='text'
        passwordInput.value='请再次输入密码'
        passwordInput.style.color='red'
    }

    if(input_code.value.trim()===''){
        event.preventDefault()
        input_code.value='不能为空'
        input_code.style.color='red'

    }
    if(input_email.value.trim()==='请输入邮箱地址'){
        event.preventDefault()
    }
    if(inputPassword.value.trim()==='请输入密码'){
        event.preventDefault()

    }
    if(passwordInput.value.trim()==='请再次输入密码'){
        event.preventDefault()

    }

    if(input_code.value.trim()==='不能为空'){
        event.preventDefault()


    }
return true;
}
function sendCode(email,btn) {

    const filteredEmail=email.replace(/<[^>]*>/g, '')
    console.log(btn)


    if(filteredEmail){
        $.ajax({
            url:  '/forgetPassword/sendCode',
            type: 'POST',
            data: {email: email},
            success: function(result){
                showCustomAlert('获取验证码成功',3000)
                refresh(btn,60);
            },
            error:function(error){
                showCustomAlert('请求出错: ' + error.status + ' ' + error.statusText, 3000);
                console.log(error);
            }

        })
    }else{
        showCustomAlert('邮箱为空，请输入邮箱',3000)
    }

}
inputPassword.addEventListener('focus',function(){

    if(this.value==='请输入密码'){
        this.value=''
        this.style.color='#333'

    }
})
input_email.addEventListener('focus',function(){
    if(this.value==='请输入邮箱地址'){
        this.value=''
        this.style.color='#333'
    }
})
passwordInput.addEventListener('focus',function(){
    if(this.value==='请再次输入密码'){
        this.value=''
        this.style.color='#333'
    }
})

input_code.addEventListener('focus',function(){
    if(this.value==='不能为空'){
        this.value=''
        this.style.color='#333'
    }
})

function refresh(btn,initialCountdown){
    let countdown=initialCountdown;
    let  timer;
    console.log(btn+"refresh")
    btn.disabled=true;
    btn.style.pointerEvents = 'none'
    /*  let time = localStorage.getItem("time"); */
    console.log(this)
    btn.textContent=`${countdown}s后重发`
    timer=setInterval(()=>{
        countdown--
        localStorage.setItem("time", countdown)
        if(countdown>0){
            btn.textContent=`${countdown}s后重发`
        }else{
            localStorage.clear();
            clearInterval(timer)
            btn.disabled=false
            // btn.classList.add('.getCode')
            btn.textContent=`获取验证码`
            btn.style.pointerEvents = 'auto'
        }
    },1000)
}
document.addEventListener('DOMContentLoaded', function () {
    let getCode = document.querySelector('.get-code');
    let btn = getCode;
    // 页面加载时检查是否有剩余时间
    let time = localStorage.getItem("time");
    if (time && parseInt(time) > 0) {
        refresh(btn, parseInt(time));
    }
})